package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.CustomerItem;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.city.CharacterParser;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.adapter.CustomManageAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.adapter.CustomerLetterComparator;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.SideBar;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/5/10.
 * Desc   : 顾客管理
 */

public class AlertCustomerActivity extends BaseActivity {

    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.txt_left)
    TextView txtLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.customer_listview)
    ListView customerListview;
    @BindView(R.id.customer_dialog)
    TextView customerDialog;
    @BindView(R.id.sidebar)
    SideBar sidebar;

    private CharacterParser characterParser;
    private CustomManageAdapter adapter;
    private List<CustomerItem> sourceDateList;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_alert_customer);
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void init() {
        imgLeft.setImageResource(R.mipmap.icon_left);
        txtTitle.setText("顾客管理");
        characterParser = CharacterParser.getInstance();

        RetrofitSource.createApi(HttpPostService.class)
                .tech_user(SharedPreferencesUtil.getsInstances(this).getLoginInfo().getToken())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<CustomerItem>>>(this) {
                    @Override
                    public void onSuccess(ResponseBase<List<CustomerItem>> response) {
                        sourceDateList = filledData(response.getData());
                        Collections.sort(sourceDateList, new CustomerLetterComparator());
                        adapter = new CustomManageAdapter(AlertCustomerActivity.this, sourceDateList);
                        customerListview.setAdapter(adapter);
                        sidebar.setTextView(customerDialog);
                        sidebar.setOnTouchingLetterChangedListener(s -> {
                            int position = adapter.getPositionForSection(s.charAt(0));
                            if (position != -1) {
                                customerListview.setSelection(position + 1);
                            }
                        });
                    }
                });
        customerListview.setOnItemClickListener((parent, view, position, id) -> {
            CustomerItem item = sourceDateList.get(position);
            startActivity(CustomerDetailActivity.createIntent(this, item.getU_id(), item.getUser_nicename(), item.getMobile()));
        });
    }

    @OnClick(R.id.ll_leftview)
    public void onViewClicked() {
        finish();
    }

    private List<CustomerItem> filledData(List<CustomerItem> list) {
        ArrayList<String> indexString = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            CustomerItem bean = list.get(i);
            String pinyin = characterParser.getSelling(bean.getUser_nicename());
            String sortString = pinyin.substring(0, 1).toUpperCase();
            if (sortString.matches("[A-Z]")) {
                bean.setSortLetters(sortString.toUpperCase());
                if (!indexString.contains(sortString)) {
                    indexString.add(sortString);
                }
            }
        }
        Collections.sort(indexString);
        sidebar.setIndexText(indexString);
        return list;
    }
}
